package com.kt.uokos.yimo.service.impl;

import com.github.pagehelper.PageHelper;
import com.kt.uokos.yimo.example.OrderExample;
import com.kt.uokos.yimo.mapper.OrderMapper;
import com.kt.uokos.yimo.service.OrderService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: TODO
 * @className: OrderServiceImpl.java
 * @author: Kang Hao
 * @date: 2022/4/30 15:34
 **/
@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;

    @Override
    public Object list(String nickname, String consignee, String orderSn, LocalDateTime start, LocalDateTime end, List<Short> orderStatusArray, Integer page, Integer limit, String sort, String order) {
        //将查询条件放入list当中
        ArrayList<Object> list = new ArrayList<>();

        if (!StringUtils.isEmpty(nickname)){
            list.add("u.nickname like '%" + nickname +"%'");
        }

        if (!StringUtils.isEmpty(consignee)){
            list.add("o.consignee like '%" + consignee +"%'");
        }

        if (!StringUtils.isEmpty(orderSn)){
            list.add("o.order_sn = '"+orderSn+"'");
        }

        DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        if (start !=null){
            list.add("o.add_time >= '"+ timeFormatter.format(start) + "'");
        }

        if (end != null){
            list.add("o.end_time <= '"+ timeFormatter.format(end) +"'");
        }

        if (orderStatusArray != null && orderStatusArray.size() > 0){
            //collectionToDelimitedString集合转字符串
            list.add("o.order_status in (" + StringUtils.collectionToDelimitedString(orderStatusArray,",") + ")");
        }

        list.add("o.deleted = 0 and og.deleted = 0");

        //将这个条件集合继续转为字符串
        StringUtils.collectionToDelimitedString(list,"and");

        String orderByClause = null;

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)){
            orderByClause ="o." + sort +" "+ order +", o.id desc";
        }

        PageHelper.startPage(page, limit);



        return null;
    }
}
